//
//  GPBaseViewController.swift
//  SeaTransportManage
//
//  Created by guopengwen on 2021/11/26.
//

import UIKit

class GPBaseViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.init(hexString: "#F7F7F7", alpha: 1)
        navigationController?.navigationBar.isTranslucent = false
        // 设置 navigationBar 的背景色
        setNavBarBackgroundColor()
        addBackBarButton()
    }
    
    // 设置 navigationBar 的背景色
    func setNavBarBackgroundColor () {
        let bgColor = COLOR_HEX(hexString: kNavBarBackgroundColor, alpha: 1)
        if #available(iOS 13.0, *) {
            let navBar = UINavigationBarAppearance.init()
            navBar.backgroundColor = bgColor;
            navBar.backgroundEffect = nil;
            navBar.shadowImage = UIImage()
            navBar.shadowColor = bgColor
            navBar.titleTextAttributes = [.foregroundColor : UIColor.white]
            navigationController?.navigationBar.scrollEdgeAppearance = navBar
            navigationController?.navigationBar.standardAppearance = navBar
        } else {
            navigationController?.navigationBar.setBackgroundImage(COLOR_IMAGE(kNavBarBackgroundColor, 1), for: UIBarMetrics.default)
            // 隐藏 navigationBar 底部的线条
            navigationController?.navigationBar.shadowImage = UIImage()
            // 修改 navigationBar 的 title 的颜色
            navigationController?.navigationBar.titleTextAttributes = [.foregroundColor : UIColor.white]
        }
    }
    
    func setNavBarTitle(_ title: String) {
        navigationItem.title = title
    }
    
    func addTopBGView() {
        let bg_Height = relativeHeight(w: 375, h: 181)
        view.addSubview(topBGView)
        topBGView.snp.makeConstraints { (make) in
            make.top.equalTo(0)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(bg_Height)
        }
    }
    
    func addBackBarButton() {
        let viewControllers = self.navigationController?.viewControllers
        if let count = viewControllers?.count, count > 1 {
        let leftButton = UIButton(frame:CGRect(x:0, y:0, width:80, height:30))
            leftButton.backgroundColor = UIColor.clear
            leftButton.contentHorizontalAlignment = .left
            leftButton.contentEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
            leftButton.setImage(UIImage(named:"arrow-up"), for: .normal)
            leftButton.addTarget(self, action: #selector(navBack), for: .touchUpInside)
            navigationItem.leftBarButtonItem = UIBarButtonItem(customView: leftButton)
       }
   }
    
    func addRightBarButtonItem(_ title: String) {
        let rButton = UIButton(frame:CGRect(x:0, y:0, width:60, height:30))
        rButton.backgroundColor = UIColor.clear
        rButton.contentHorizontalAlignment = .right
        rButton.setTitle(title, for: UIControl.State.normal)
        rButton.setTitleColor(UIColor.white, for: UIControl.State.normal)
        rButton.contentEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        rButton.addTarget(self, action: #selector(clickRightBarButton), for: .touchUpInside)
        navigationItem.rightBarButtonItem = UIBarButtonItem(customView: rButton)
        
    }
    
    @objc func clickRightBarButton() {
        rightBarButtonAction()
    }
   
    func rightBarButtonAction() {}
    
    @objc func navBack() {
        backBarButtoAction()
    }
    
    func backBarButtoAction() {
        navigationController?.popViewController(animated: true)
    }
    
    func showLoading(_ info: String) {
        MTProgressHUD.showMessage(info, toView: self.view)
    }
    
    func hideLoading() {
        MTProgressHUD.hideHUDForView(self.view)
    }
    
    func toastInfo(_ info: String) {
        MTProgressHUD.showInfo(info, toView: self.view)
    }
    
    func toastError(_ info: String) {
        MTProgressHUD.showError(info, toView: self.view)
    }
    
    func toastSuccess(_ info: String) {
        MTProgressHUD.showSuccess(info, toView: self.view)
    }
    
    lazy var topBGView: UIImageView = {
        let imgView = UIImageView.init(image: UIImage.init(named: "home-bg"))
        return imgView
    }()
}
